package com.rqbl.controller;


import com.rqbl.entity.Bar;
import com.rqbl.entity.User;
import com.rqbl.myutils.PageBean;
import com.rqbl.service.BarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Controller
@RequestMapping("/bar")
@CrossOrigin
public class BarController {
    @Autowired
    private BarService barService;

    /**
     * 获取所有的趣点（分页）
     * @param page
     * @return
     */
    @RequestMapping("getAllBarByPage")
    @ResponseBody
    public List<Bar> getAllBarByPage(int page) {
        PageBean pageBean = new PageBean();
        System.out.println("page:" + page);
        pageBean.setPage(page);
        pageBean.setPageSize(8);
        return barService.getBarByPage(pageBean);
    }

    /**
     * 测试方法
     * @return
     */

    @RequestMapping("test")
    @ResponseBody
    public String test() {
        return "test";
    }


    /**
     * 根据bid获取趣点信息，并跳转到趣点的主界面
     * @param bid
     */
    @RequestMapping("forwardBarById")
    public String forwardBarById(Integer bid, HttpServletRequest request) {
        request.setAttribute("bar", barService.getBarById(bid));
        return "forward:/forward/barIndexPage";
    }

    /**
     * 获取趣点信息，但不进行跳转
     * @return
     */
    @RequestMapping("getBarByBid")
    @ResponseBody
    public Bar selectBarByBid(Integer bid) {
        return barService.getBarById(bid);
    }

    /**
     * 上传趣点图片
      * @param photo
     * @return
     * @throws IOException
     */
    @RequestMapping("addBarImg")
    @ResponseBody
    public ModelAndView upload(MultipartFile photo) throws IOException {
        ModelAndView mv = new ModelAndView();
        //判断用户是否上传了文件
        if (!photo.isEmpty()) {

            //文件上传的地址
            String path = ResourceUtils.getURL("classpath:").getPath() + "static/upload";
            String realPath = path.replace('/', '\\').substring(1, path.length());
            //用于查看路径是否正确
            System.out.println(realPath);

            //获取文件的名称
            final String fileName = photo.getOriginalFilename();

            //限制文件上传的类型
            String contentType = photo.getContentType();
            if ("image/jpeg".equals(contentType) || "image/jpg".equals(contentType) || "image/png".equals(contentType)) {
                File file = new File(realPath, fileName);
                //完成文件的上传
                photo.transferTo(file);
                System.out.println("图片上传成功!");
                String path01 = "../barImg/" + fileName;
                mv.addObject("path", path01);
                mv.setViewName("lookphoto");
                return mv;
            } else {
                System.out.println("上传失败！");
                mv.setViewName("upload");
                return mv;
            }
        } else {
            System.out.println("上传失败！");
            mv.setViewName("upload");
            return mv;
        }
    }

    @RequestMapping("barDetail")
    public String barDetail(Integer bid, HttpServletRequest request) {
        request.setAttribute("bar", barService.getBarById(bid));
        return "forward:/forward/barDetailPage";
    }

    /**
     * 获得与uid对应用户有关的所有趣点信息
     */
    @RequestMapping("getAllBarByUid")
    @ResponseBody
    public List<Bar> getAllBarByUid(Integer uid) {
        return barService.getAllBarByUid(uid);
    }

    /**
     * 更新吧的公告信息
     */
    @RequestMapping("updateBarAnnounce")
    @ResponseBody
    public String updateBarAnnounce(Integer bid, String notice) {
        barService.updateBarAnnounce(bid, notice);
        return "success";
    }

    /**
     * 更新图片路径
     */
    @RequestMapping("updateBarImgByBid")
    @ResponseBody
    public String updateBarImgByBid(Integer bid, String url) {
        barService.updateBarImgByBid(bid, url);
        return "success";
    }

    @RequestMapping("updateBarInfo")
    @ResponseBody
    public String updateBarInfo(Integer bid, String bname, String bdescribe) {
        barService.updateBarInfo(bid, bname, bdescribe);
        return "success";
    }

}
